/********************************************************************
# Copyright 2016-2017 Daniel 'grindhold' Brendle
#
# This file is part of liboparl.
#
# liboparl is free software: you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public License
# as published by the Free Software Foundation, either
# version 3 of the License, or (at your option) any later
# version.
#
# liboparl is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
# PURPOSE. See the GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with liboparl.
# If not, see http://www.gnu.org/licenses/.
*********************************************************************/

using OParl;

namespace OParlTest {
    public class BodyTest {
        private static GLib.HashTable<string,string> test_input;

        private static void init() {
            BodyTest.test_input = new GLib.HashTable<string,string>(GLib.str_hash, GLib.str_equal);

            BodyTest.test_input.insert("https://oparl.example.org/", Fixtures.system_sane);
            BodyTest.test_input.insert("https://oparl.example.org/bodies", Fixtures.body_list_sane);
            BodyTest.test_input.insert("https://oparl.example.org/body/0/organizations/", Fixtures.organization_list_sane);
            BodyTest.test_input.insert("https://oparl.example.org/body/0/meetings/", Fixtures.meeting_list_sane);
            BodyTest.test_input.insert("https://oparl.example.org/body/0/people/", Fixtures.person_list_sane);
            BodyTest.test_input.insert("https://oparl.example.org/body/0/papers/", Fixtures.paper_list_sane);
        }

        public static void add_tests () {
            BodyTest.init();

            Test.add_func ("/oparl/body/sane_input", () => {
                var client = new Client();
                TestHelper.mock_connect(ref client, BodyTest.test_input, null);
                System s;
                try {
                    s = client.open("https://oparl.example.org/");
                } catch (ParsingError e) {
                    GLib.assert_not_reached();
                }

                try {
                    Body b = s.get_body().nth_data(0);

                    assert (b.id == "https://oparl.example.org/body/0");
                    assert (b.get_system() != null);
                    assert (b.get_system() is OParl.System);
                    assert (b.contact_email == "ris@beispielstadt.de");
                    assert (b.contact_name == "RIS-Betreuung");
                    assert (b.ags == "05315000");
                    assert (b.rgs == "053150000000");
                    assert (b.classification == "Kreisfreie Stadt");
                    assert (b.equivalent.length == 2);
                    assert (b.equivalent[0] == "http://d-nb.info/gnd/2015732-0");
                    assert (b.equivalent[1] == "http://dbpedia.org/resource/Cologne");
                    assert (b.short_name == "Köln");
                    assert (b.name == "Stadt Köln, kreisfreie Stadt");
                    assert (b.website == "http://www.beispielstadt.de/");
                    assert (b.license == "http://creativecommons.org/licenses/by/4.0/");
                    assert (b.license_valid_since.to_string() == "2015-01-01T14:28:31+0000");
                    assert (b.oparl_since.to_string() == "2014-01-01T14:28:31+0000");
                    assert (b.get_organization() != null);
                    assert (b.get_organization().nth_data(0) != null);
                    assert (b.get_organization().nth_data(0) is Organization);
                    assert (b.get_person() != null);
                    assert (b.get_person().nth_data(0) != null);
                    assert (b.get_person().nth_data(0) is Person);
                    assert (b.get_meeting() != null);
                    assert (b.get_meeting().nth_data(0) != null);
                    assert (b.get_meeting().nth_data(0) is Meeting);
                    assert (b.get_paper() != null);
                    assert (b.get_paper().nth_data(0) != null);
                    assert (b.get_paper().nth_data(0) is Paper);
                } catch (ParsingError e) {
                    GLib.assert_not_reached();
                }
            });

            Test.add_func ("/oparl/body/wrong_id_type", () => {
                var client = new Client();
                TestHelper.mock_connect(ref client, BodyTest.test_input, "\"https://oparl.example.org/body/0\"");
                try {
                    System s = client.open("https://oparl.example.org/");
                    s.get_body().nth_data(0);
                    GLib.assert_not_reached();
                } catch (ParsingError e) {
                    assert(e.message.contains("'id'"));
                }
            });

            Test.add_func ("/oparl/body/wrong_system_type", () => {
                var client = new Client();
                TestHelper.mock_connect(ref client, BodyTest.test_input, "\"https://oparl.example.org/\"");
                try {
                    System s = client.open("https://oparl.example.org/");
                    s.get_body().nth_data(0);
                    GLib.assert_not_reached();
                } catch (ParsingError e) {
                    assert(e.message.contains("'id'"));
                }
            });

            Test.add_func ("/oparl/body/wrong_contact_email_type", () => {
                var client = new Client();
                TestHelper.mock_connect(ref client, BodyTest.test_input, "\"ris@beispielstadt.de\"");
                try {
                    System s = client.open("https://oparl.example.org/");
                    s.get_body().nth_data(0);
                    GLib.assert_not_reached();
                } catch (ParsingError e) {
                    assert(e.message.contains("'contactEmail'"));
                }
            });

            Test.add_func ("/oparl/body/wrong_contact_name_type", () => {
                var client = new Client();
                TestHelper.mock_connect(ref client, BodyTest.test_input, "\"RIS-Betreuung\"");
                try {
                    System s = client.open("https://oparl.example.org/");
                    s.get_body().nth_data(0);
                    GLib.assert_not_reached();
                } catch (ParsingError e) {
                    assert(e.message.contains("'contactName'"));
                }
            });

            Test.add_func ("/oparl/body/wrong_ags_type", () => {
                var client = new Client();
                TestHelper.mock_connect(ref client, BodyTest.test_input, "\"05315000\"");
                try {
                    System s = client.open("https://oparl.example.org/");
                    s.get_body().nth_data(0);
                    GLib.assert_not_reached();
                } catch (ParsingError e) {
                    assert(e.message.contains("'ags'"));
                }
            });

            Test.add_func ("/oparl/body/wrong_rgs_type", () => {
                var client = new Client();
                TestHelper.mock_connect(ref client, BodyTest.test_input, "\"053150000000\"");
                try {
                    System s = client.open("https://oparl.example.org/");
                    s.get_body().nth_data(0);
                    GLib.assert_not_reached();
                } catch (ParsingError e) {
                    assert(e.message.contains("'rgs'"));
                }
            });

            Test.add_func ("/oparl/body/wrong_classification_type", () => {
                var client = new Client();
                TestHelper.mock_connect(ref client, BodyTest.test_input, "\"Kreisfreie Stadt\"");
                try {
                    System s = client.open("https://oparl.example.org/");
                    s.get_body().nth_data(0);
                    GLib.assert_not_reached();
                } catch (ParsingError e) {
                    assert(e.message.contains("'classification'"));
                }
            });

            Test.add_func ("/oparl/body/wrong_equivalent_type", () => {
                // TODO: implement
            });

            Test.add_func ("/oparl/body/wrong_website_type", () => {
                var client = new Client();
                TestHelper.mock_connect(ref client, BodyTest.test_input, "\"http://www.beispielstadt.de/\"");
                try {
                    System s = client.open("https://oparl.example.org/");
                    s.get_body().nth_data(0);
                    GLib.assert_not_reached();
                } catch (ParsingError e) {
                    assert(e.message.contains("'website'"));
                }
            });

            Test.add_func ("/oparl/body/wrong_license_type", () => {
                var client = new Client();
                TestHelper.mock_connect(ref client, BodyTest.test_input, "\"http://creativecommons.org/licenses/by/4.0/\"");
                try {
                    System s = client.open("https://oparl.example.org/");
                    s.get_body().nth_data(0);
                    GLib.assert_not_reached();
                } catch (ParsingError e) {
                    assert(e.message.contains("'license'"));
                }
            });

            Test.add_func ("/oparl/body/wrong_license_valid_since_type", () => {
                var client = new Client();
                TestHelper.mock_connect(ref client, BodyTest.test_input, "\"2015-01-01T14:28:31.568+0000\"");
                try {
                    System s = client.open("https://oparl.example.org/");
                    s.get_body().nth_data(0);
                    GLib.assert_not_reached();
                } catch (ParsingError e) {
                    assert(e.message.contains("'licenseValidSince'"));
                }
            });

            Test.add_func ("/oparl/body/wrong_oparl_since_type", () => {
                var client = new Client();
                TestHelper.mock_connect(ref client, BodyTest.test_input, "\"2014-01-01T14:28:31.568+0000\"");
                try {
                    System s = client.open("https://oparl.example.org/");
                    s.get_body().nth_data(0);
                    GLib.assert_not_reached();
                } catch (ParsingError e) {
                    assert(e.message.contains("'oparlSince'"));
                }
            });
        }
    }
}
